﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using System;

using seh = Sys.EventHandler;
using sea = Sys.EventArgs;

namespace SBT
{
    public enum Orientation
    {
        Vertical =1,
        Horizontal =2
    }
    

    public class StackPanel : AgLayoutManager
    {
        private bool m_sizeToContent = true;

        private seh m_childSizeChangedHandler;
        private seh m_widthChangedHandler;
        private seh m_heightChangedHandler;
        protected override void OnUIElementAdded(UIElement item)
        {
            base.OnUIElementAdded(item);
            if (IsInitialized)
            {
                AgBehavior behavior = AgBehavior.FindBehaviorByUIElement(item);
                if (!(bool)(object)behavior)
                {
                    behavior = new AgBehavior();
                    behavior.Element = item;
                    behavior.Plugin = Plugin;
                    behavior.Initialize();
                }
                
                behavior.add_widthChanged(m_childSizeChangedHandler);
                behavior.add_heightChanged(m_childSizeChangedHandler);
                
                
            }
        }
        protected override void OnUIElementRemoved(UIElement item)
        {
            base.OnUIElementRemoved(item);
            if (IsInitialized)
            {
                AgBehavior behavior = AgBehavior.FindBehaviorByUIElement(item);
                if (!(bool)(object)behavior)
                {
                    behavior = new AgBehavior();
                    behavior.Element = item;
                    behavior.Plugin = Plugin;
                    behavior.Initialize();
                }
                
                behavior.remove_widthChanged(m_childSizeChangedHandler);
                behavior.remove_heightChanged(m_childSizeChangedHandler);
                
            }
        }
        public bool SizeToContent
        {
            get
            {
                return m_sizeToContent;

            }
            set
            {
                m_sizeToContent = value;
            }
        }
        public override void Initialize()
        {
            base.Initialize();
            m_childSizeChangedHandler = OnChildSizeChanged;
            m_widthChangedHandler = OnWidthChanged;
            m_heightChangedHandler = OnHeightChanged;
            this.add_widthChanged(m_widthChangedHandler);
            this.add_heightChanged(m_heightChangedHandler);
            for (int i = 0; i < UnderlyingCanvas.Children.Count; i++)
            {
                UIElement item = UnderlyingCanvas.Children.GetItem(i);
                AgBehavior behavior = AgBehavior.FindBehaviorByUIElement(item);
                if (!(bool)(object)behavior)
                {
                    behavior = new AgBehavior();
                    behavior.Element = item;
                    behavior.Plugin = Plugin;
                    behavior.Initialize();
                }
                
                    behavior.add_widthChanged(m_childSizeChangedHandler);
                    behavior.add_heightChanged(m_childSizeChangedHandler);
                
            }
        }
        private Orientation m_orientation;
        public Orientation Orientation
        {
            get { return m_orientation; }
            set 
            {
                if (m_orientation != value)
                {
                    m_orientation = value;
                    if (IsInitialized)
                        InvalidateLayout();
                }
            }
        }
        public StackPanel()
        {
            m_orientation = Orientation.Vertical;
        }
        private void OnHeightChanged(object sender, sea args)
        {
            if (Orientation == Orientation.Horizontal)
                InvalidateLayout();
        }
        private void OnWidthChanged(object sender, sea args)
        {
            if (Orientation == Orientation.Vertical)
                InvalidateLayout();
        }
        private void OnChildSizeChanged(object sender, sea args)
        {
            InvalidateLayout();
        }
        public override void InvalidateLayout()
        {
            base.InvalidateLayout();
            double offset = 0;
            for (int i = 0; i < UnderlyingCanvas.Children.Count; i++)
            {
                
                UIElement element = UnderlyingCanvas.Children.GetItem(i);
                AgBehavior behavior = AgBehavior.FindBehaviorByUIElement(element);
                if (element.Visibility == Visibility.Visible)
                {
                    if (Orientation == Orientation.Horizontal)
                    {
                        if (behavior != null)
                        {
                            behavior.Height = Height;
                            behavior.Left = offset;
                            
                        }
                        else
                        {
                            element.Height = Height;
                            element.SetValue("Canvas.Left", offset);
                            
                        }
                    }
                    else
                    {
                        if (behavior != null)
                        {
                            behavior.Width = Width;
                            behavior.Top = offset;
                            
                        }
                        else
                        {
                            element.Width = Width;
                            element.SetValue("Canvas.Top", offset);
                            
                        }
                    }
                    try
                    {
                        TextBlock asTb =  (TextBlock)element;
                        asTb.Text = asTb.Text;
                    }
                    catch
                    {
                    }
                    DBounds bounds = GetItemBounds(i);
                    if (Orientation == Orientation.Horizontal)
                        offset += bounds.Width;
                    else
                        offset += bounds.Height;
                }
            }
            if (SizeToContent)
            {
                if (Orientation == Orientation.Horizontal)
                    this.Width = offset;
                else
                    this.Height = offset;
            }
        }
        
        
    }
}